package com.sm.service;

import com.sm.dao.ShareImgMapper;
import com.sm.model.ShareImg;
import com.sm.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ShareImgService {

    @Autowired
    private ShareImgMapper shareImgMapper;

    public String getCurrentImg() {
        //当前时间
        String currentTime = cn.hutool.core.date.DateUtil.format(new Date(), "HH:mm:ss");
        String defaultImg;
        List<ShareImg> list = shareImgMapper.list();
        List<ShareImg> collect = list.stream().limit(1).collect(Collectors.toList());
        defaultImg = collect.get(0) != null ? collect.get(0).getImgUrl() : "";
        for (ShareImg shareImg : list) {
            try {
                if (DateUtil.isEffectiveDate(currentTime, shareImg.getBeginTime(), shareImg.getEndTime())) {
                    return shareImg.getImgUrl();
                }
            } catch (ParseException e) {
                return defaultImg;
            }
        }
        return defaultImg;
    }
}
